/**
 * 
 */
package com.probiz.estore.core.view;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;

import com.probiz.estore.core.view.json.AjaxJsonView;

/**
 * 支持controller返回json方法的异常处理
 * 
 * @author pengzhirong
 * 
 * @sinnce V3.5
 *
 */
public class ProbizMappingExceptionResolver extends SimpleMappingExceptionResolver {
	@Override
	public ModelAndView resolveException(
		    HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
		//X-Requested-With由JQuery封装而来
		if("XMLHttpRequest".equals(request.getHeader("X-Requested-With")) || "XMLHttpRequest".equals(request.getHeader("x-requested-with"))){
			response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			response.addHeader("error",  ex.getMessage());
			return new ModelAndView(new AjaxJsonView());
		}else{
			if (shouldApplyTo(request, handler)) {request.getHeaderNames();
				return doResolveException(request, response, handler, ex);
			}
			else {
				return null;
			}
		}
	}
}
